<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'

require('./spider') // echarts theme
import {debounce} from '@/utils'
import china from './map/json/china.json'

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    optionTitleText: {
      type: String,
      default: ''
    },
    max: {
      type: Number,
      default: 0
    },
    data: {
      type: Array,
      default: []
    },
  },
  data() {
    return {
      chart: null,
      opinion: {
        title: {
          text: this.optionTitleText,
          padding: 10,
        },
        backgroundColor: '#fff',
        visualMap: {
          min: 0,
          max: this.max,
          left: 'left',
          top: 'bottom',
          text: ['High','Low'],
          seriesIndex: [1],

          calculable : true
        },
        geo: {
          map: 'china',
          roam: false,
          label: {
            normal: {
              show: true,
              textStyle: {
                color: 'rgba(0,0,0,0.4)'
              }
            }
          },
          // itemStyle: {
          //   normal:{
          //     borderColor: 'rgba(0, 0, 0, 0.2)'
          //   },
          //   emphasis:{
          //     areaColor: null,
          //     shadowOffsetX: 0,
          //     shadowOffsetY: 0,
          //     shadowBlur: 20,
          //     borderWidth: 0,
          //     shadowColor: 'rgba(0, 0, 0, 0.5)'
          //   }
          // }
        },
        series : [
          {
            type: 'scatter',
            coordinateSystem: 'geo',
            symbolSize: 20,
            symbol: 'none',
            symbolRotate: 35,
            label: {
              normal: {
                formatter: '{b}',
                position: 'right',
                show: false
              },
              emphasis: {
                show: true
              }
            },
            itemStyle: {
              normal: {
                color: '#F06C00'
              }
            }
          },
          {
            name: '舆情数量',
            type: 'map',
            geoIndex: 0,
            data:this.data,
          }
        ]
      }
    }
  },
  watch: {

    optionTitleText(newValue, oldValue) {
      this.opinion.title.text = newValue
      this.chart.setOption(this.opinion);
    },
    max(newValue, oldValue) {
      this.opinion.visualMap.max = newValue
      this.chart.setOption(this.opinion);
    },
    data(newValue, oldValue) {
      this.opinion.series[1].data= newValue
      this.chart.setOption(this.opinion);
    },
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      echarts.registerMap('china', china);
      this.chart = echarts.init(this.$el, 'spider')
      this.chart.setOption(this.opinion)
      this.chart.on('click', param => {
        this.$emit('clickEvent',param.name)
      })
    }
  }
}
</script>
